﻿Public Class NguoiNhanTinDAO

    Private connect As ConnectDB
    Private sqlstr As String


    Public Sub New()
        connect = New ConnectDB()
    End Sub

    Public Function IsUpNNT(ByVal emp As NguoiNhanTinDTO, ByVal flag As Boolean)
        Dim ma As String = emp.Mã_NT
        Dim hoten As String = emp.Họ_tên
        Dim email As String = emp.Email
        Dim gia As String = emp.Giá_tiền
        Dim makv As String = emp.Mã_khu_vực
        Dim maln As String = emp.Mã_loại_nhà
        '-------------------------------------------'
        If flag = True Then
            sqlstr = "INSERT NGUOINHANTIN (HoTenNN, EmailNN, Gia, MaKhuVuc, MaLoaiNha)" & _
                        " VALUES('" & hoten & "', '" & email & "', '" & _
                                        gia & "', '" & makv & "', '" & maln & "')"
        Else
            sqlstr = "UPDATE NGUOINHANTIN SET  HoTenNN  = '" & hoten & _
                                        "', EmailNN   = '" & email & _
                                        "', Gia      = '" & gia & _
                                        "',  MaKhuVuc      = '" & makv & _
                                        "',  MaLoaiNha      = '" & maln & _
                                    "' WHERE MaNguoiNhan  = '" & ma & "'"

        End If
        Return connect.Execute(sqlstr)
    End Function

    Public Function DeleteNNT(ByVal emp As NguoiNhanTinDTO)
        Dim EmpID As String = emp.Mã_NT
        sqlstr = "DELETE NGUOINHANTIN WHERE MaNguoiNhan = '" & EmpID & "'"
        Return connect.Execute(sqlstr)
    End Function



    Public Function Getdata() As List(Of NguoiNhanTinDTO)
        Dim i As Integer
        Dim maNT, ten, email, gia, makv, maln As String
        Dim emp As NguoiNhanTinDTO
        Dim nnList As New List(Of NguoiNhanTinDTO)
        Dim dt As New DataTable
        dt = connect.GetData("Select * From NGUOINHANTIN")

        For i = 0 To dt.Rows.Count() - 1
            maNT = dt.Rows(i)("MaNguoiNhan").ToString
            ten = dt.Rows(i)("HoTenNN").ToString
            email = dt.Rows(i)("EmailNN").ToString
            gia = dt.Rows(i)("Gia").ToString
            makv = dt.Rows(i)("MaKhuVuc").ToString
            maln = dt.Rows(i)("MaLoaiNha").ToString

            emp = New NguoiNhanTinDTO(maNT, ten, email, gia, makv, maln)
            nnList.Add(emp)
        Next
        Return nnList
    End Function

End Class
